﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Pan
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["logout"] != null)
            {
                Session.Clear();
                if(Convert.ToString(Request.UrlReferrer) != string.Empty)
                    Response.Redirect(Request.UrlReferrer.PathAndQuery);
                else
                    Response.Redirect("/login.aspx");
            }
            if (Convert.ToInt32(Master.uid) > 0)
            {
                Response.Redirect("/disk.aspx");
            }
        }

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PanConnectionString"].ToString();
            Master.RespLabel.Text = "";
            try
            {
                conn.Open();
                // 登录
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "Login";
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 255).Value = Username.Text.Trim();
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.Read() && sdr["pwd"].ToString() == Password.Text.Trim())
                {
                    Session["uid"] = sdr["id"];
                    Session["uname"] = sdr["name"];
                    Session["root"] = sdr["root"];
                    Response.Redirect("/disk.aspx");
                }
                else
                {
                    Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.alert('用户名或密码错误')})</script>";
                }
                sdr.Close();
            }
            catch (Exception err)
            {
                Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.alert('"+ err.Message +"')})</script>";
            }
            finally
            {
                conn.Close();
            }
        }
    }
}